#!/bin/bash
echo
echo "-----------------------------------------------------------------------"
echo "Thinkpad 13 Chromobook fixes installation script for Elementary OS Juno"
echo "-----------------------------------------------------------------------"
echo "Read more: https://github.com/Backpackstudio/chromebook-sentry."
echo
read -p "Are you sure about altering your system? (y/n)?" CONT

if [ "$CONT" = "y" ]; then
	echo
	echo "-----------------------------------------------------------------------"
	echo "Installing mappings for Chromobook keys"
	echo "-----------------------------------------------------------------------"
	md51="$(md5sum "/usr/share/X11/xkb/symbols/pc")"
	md51="${md51%% *}"
	echo "MD5 of current mappings: ${md51}"
	echo "Making backup of current mappings file..."
	cp -n "/usr/share/X11/xkb/symbols/pc" "/usr/share/X11/xkb/symbols/pc.${md51}.bck"
	cd /usr/share/X11/xkb/symbols/
	echo "Downloading and replacing mappings file..."
	wget https://raw.githubusercontent.com/Backpackstudio/chromebook-sentry/master/mods/usr/share/X11/xkb/symbols/pc -O pc
	md52="$(md5sum "/usr/share/X11/xkb/symbols/pc")"
	md52="${md52%% *}"
	echo "MD5 of installed mappings: ${md52}. (Previous: ${md51}.)"
	echo
	echo "-----------------------------------------------------------------------"
	echo "Installing audio drivers and settings for Sentry Chromobook"
	echo "-----------------------------------------------------------------------"
	cd /lib/firmware/
	echo "Downloading and installing audio firmware..."
	wget https://raw.githubusercontent.com/Backpackstudio/chromebook-sentry/master/mods/lib/firmware/dfw_sst.bin -O dfw_sst.bin
	md53="$(md5sum "/lib/firmware/dfw_sst.bin")"
	md53="${md53%% *}"
	echo "MD5 of installed audio firmware (dfw_sst.bin): ${md53}."
	echo "Installing Sentry Chromobook audio hardware definitons for ALSA"
	rm -d /usr/share/alsa/ucm/Google-sentry-1.0-sentry
	mkdir -p /usr/share/alsa/ucm/sklnau8825max/
	cd /usr/share/alsa/ucm/sklnau8825max/
	echo "Downloading and installing audio configuration..."
	wget https://raw.githubusercontent.com/Backpackstudio/chromebook-sentry/master/mods/usr/share/alsa/ucm/sklnau8825max/Google-sentry-1.0-sentry.conf -O Google-sentry-1.0-sentry.conf
	wget https://raw.githubusercontent.com/Backpackstudio/chromebook-sentry/master/mods/usr/share/alsa/ucm/sklnau8825max/HiFi.conf -O HiFi.conf
	wget https://raw.githubusercontent.com/Backpackstudio/chromebook-sentry/master/mods/usr/share/alsa/ucm/sklnau8825max/sklnau8825max.conf -O sklnau8825max.conf
	ln -s /usr/share/alsa/ucm/sklnau8825max /usr/share/alsa/ucm/Google-sentry-1.0-sentry
	echo "Disabling snd-hda-intel"
	cd /etc/modprobe.d/
	wget https://raw.githubusercontent.com/Backpackstudio/chromebook-sentry/master/mods/etc/modprobe.d/snd-hda-intel.conf -O snd-hda-intel.conf
	echo
	echo "Installation complete."
	echo "Please visit https://github.com/Backpackstudio/chromebook-sentry for additional instructions."
	echo
	echo "-----------------------------------------------------------------------"
	echo "Please reboot your chromebook to load new audio drivers."
	exit
else
	echo "Installation cancelled."
	echo "Please visit https://github.com/Backpackstudio/chromebook-sentry for additional instructions."
	exit
fi